// ==UserScript==
// @name         Bear
// @namespace    https://gitee.com/linhq1999/OhMyScript
// @version      0.0.1
// @description  非官方的熊曰解码器
// @author       LinHQ
// @match        http*://*/*
// @grant        GM_openInTab
// @grant        GM_xmlhttpRequest
// @inject-into  auto
// @license MIT
// ==/UserScript==

(() => {
    let c = undefined, isDecode = !0
    // , timeStamp = new Date().getTime()

    function getKey(a: number[]) {
        for (var d = [], c = "", b = 48; 58 > b; b++)
            d.push(String.fromCharCode(b));
        for (b = 64; 91 > b; b++)
            d.push(String.fromCharCode(b));
        for (b = 0; b < a.length; b++)
            c += d[a[b]];
        return c
    }

    /**
     * 
     * @param a 加密算法
     * @param d Encode
     * @param c 密文
     * @returns 
     */
    function BearCode(c: string, a = "Bear", d = "Encode") {
        console.warn("Debugging!")
        isDecode = "Decode" == d ? !0 : !1;
        // if (500 > (new Date).getTime() - timeStamp) return !1;
        // timeStamp = (new Date).getTime();
        var h = getKey([0, 7, 12, 9, 7, 11, 11, 6, 4, 4, 15, 8]);
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://hi.pcmoe.net/bear.php",
            data: "mode=" + encodeURIComponent(a) + "&code=" + encodeURIComponent(d) + "&txt=" + encodeURIComponent(c),
            timeout: 5E3,
            headers: {
                "X-TOKEN": h,
                "X-Requested-With": "XMLHttpRequest",
                "Content-type": "application/x-www-form-urlencoded"
            },
            onload: data => console.log(data),
            onerror: console.error
        })
    }

    BearCode("贵干")
})()